package com.zrrd.gmall.list.receiver;

import com.rabbitmq.client.Channel;
import com.zrrd.gmall.list.service.SearchService;
import com.zrrd.gmall.rabbit.constant.MqConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ListReceiver {
    @Autowired
    private SearchService searchService;
    //商品上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstant.QUEUE_GOODS_UPPER,durable = "true"),
            exchange = @Exchange(value = MqConstant.EXCHANGE_DIRECT_GOODS,
                type = ExchangeTypes.DIRECT,durable = "true"),
            key = {MqConstant.ROUTING_GOODS_UPPER}
    ))
    public void upperGoods(Long skuId, Message message, Channel channel)throws IOException{
        System.out.println("搜索服务收到上架消息====>"+skuId);
        if (skuId!=null){
            searchService.upperGoods(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //商品下架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstant.QUEUE_GOODS_LOWER,durable = "true"),
            exchange = @Exchange(value = MqConstant.EXCHANGE_DIRECT_GOODS,
                    type = ExchangeTypes.DIRECT,durable = "true"),
            key = {MqConstant.ROUTING_GOODS_LOWER}
    ))
    public void lowerGoods(Long skuId, Message message, Channel channel)throws IOException{
        System.out.println("搜索服务收到下架消息====>"+skuId);
        if (skuId!=null){
            searchService.lowerGoods(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
